#include "thread.h"
int makeWork(threadPool_t *pThreadPool){
    for(int i = 0; i < pThreadPool->workNum; ++i){
        pthread_create(&pThreadPool->workArr[i],NULL,threadFunc,pThreadPool);
    }
    return 0;
}
void *threadFunc(void *arg){
    threadPool_t * pThreadPool = (threadPool_t *)arg;
    pthread_mutex_lock(&pThreadPool->mutex);
    MYSQL *mysql= mysql_init(NULL);
    MYSQL *mret = mysql_real_connect(mysql,
                                     "localhost","root","123456",
                                     "diskMysql",
                                     0,NULL,0
                                    );
    pthread_mutex_unlock(&pThreadPool->mutex);
    //NETDISK_LOG_ERROR(ret,-1,"makeMysql");


    while(1){
        train_t train;
        bzero(&train,sizeof(train_t));
        pthread_mutex_lock(&pThreadPool->mutex);
        while(pThreadPool->task.size == 0){
            pthread_cond_wait(&pThreadPool->cond,&pThreadPool->mutex);
        }
        printf("worker got a job!\n");
        int netfd = pThreadPool->task.pFront->netfd;
        outQueue(&pThreadPool->task);
        pthread_mutex_unlock(&pThreadPool->mutex);

        sharInfo_t sharInfo;
        bzero(&sharInfo,sizeof(sharInfo_t));

        int ret;
        for(int i=0;i<5;i++){
            recv(netfd,&train.status,sizeof(int),MSG_WAITALL);
            if(train.status==OD_REG){
                ret = registerAcc(netfd,mysql);
                NETDISK_LOG_ERROR(ret,-1,"registerAcc");
            }

            else if(train.status==OD_LOG){
                ret=login(netfd,&sharInfo,mysql);
                NETDISK_LOG_ERROR(ret,-1,"login")
                    if(ret==0){
                        break;
                    }
            }
        }
        printf("%s login over\n",sharInfo.user);


        while(1){
            ret=recv(netfd,&train.status,sizeof(int),MSG_WAITALL);
            printf("cmd = %d\n",train.status);
            if(ret==0){
                NETDISK_LOG_ERROR(ret,0,"exit");
                break;
            }

            switch(train.status){
            case OD_CD:
                ret=changeDir(netfd,&sharInfo,mysql);
                NETDISK_LOG_ERROR(ret,-1,"changeDir");
                break;
            case OD_LS:
                ret=mysqlls(netfd,&sharInfo,mysql);
                NETDISK_LOG_ERROR(ret,-1,"mysqlls");
                break;
            case OD_MD:
                ret=makedir(netfd,&sharInfo,mysql);
                NETDISK_LOG_ERROR(ret,-1,"makedir");
                break;
            case OD_RD:
                printf("removedir BB\n");
                ret=removedir(netfd,&sharInfo,mysql);
                NETDISK_LOG_ERROR(ret,-1,"removedir");
                break;
            case OD_RM:
                printf("RM BB\n");
                ret=rm(netfd,&sharInfo,mysql);
                NETDISK_LOG_ERROR(ret,-1,"rm");
                break;
            case OD_PWD:
                ret=pwd(netfd,&sharInfo,mysql);
                NETDISK_LOG_ERROR(ret,-1,"pwd");
                break;
            case OD_GTC:
                ret=sersend(netfd,mysql,&sharInfo);
                NETDISK_LOG_ERROR(ret,-1,"sersend");
                break;
            case OD_PTS:
                ret=serrecv(netfd,mysql,&sharInfo);
                NETDISK_LOG_ERROR(ret,-1,"serrecv");
                break;
            default:
                break;
            }
        }

        close(netfd);

        //执行任务
    }
    free(mysql);
    return 0;
}
